﻿using System;
using System.Html.DOM;

namespace RexToy.UI.Controls
{
    public class RadioButton : Control, IChoiceControl
    {
        private bool __enable = true;
        public bool Enable
        {
            get { return __enable; }
            set
            {
                if (value)
                {
                    this.__domNode.RemoveClass("ui-disabled");
                }
                else
                {
                    this.__domNode.AppendClass("ui-disabled");
                }

                this.__enable = value;
            }
        }

        private bool __checked = false;
        public bool Checked
        {
            get { return __checked; }
            set
            {
                this.__checked = value;
                var l = this.__domNode.SearchByName("lbl");
                var c = this.__domNode.SearchByName("check");
                if (this.__checked)
                {
                    if (this._activeOnCheck)
                        l.AppendClass("ui-btn-active");
                    c.ReplaceClass("ui-icon-radio-off", "ui-icon-radio-on");
                }
                else
                {
                    if (this._activeOnCheck)
                        l.RemoveClass("ui-btn-active");
                    c.ReplaceClass("ui-icon-radio-on", "ui-icon-radio-off");
                }
            }
        }

        public override bool Standalone
        {
            get
            {
                return __standalone;
            }
            set
            {
                var spn = this.__domNode.SearchByName("spn");
                var lbl = this.__domNode.SearchByName("lbl");
                if (value)
                {
                    spn.AppendClass("ui-corner-all");
                    lbl.AppendClass("ui-corner-all");
                }
                else
                {
                    spn.RemoveClass("ui-corner-all");
                    lbl.RemoveClass("ui-corner-all");
                }
                __standalone = value;
            }
        }

        private object __value = 0;
        public object Value
        {
            get { return __value; }
        }

        private bool _activeOnCheck;
        public string[] events = new string[] { "onchange" };

        public RadioButton(string text, object value, bool activeOnCheck)
        {
            this.__value = value;
            this._activeOnCheck = activeOnCheck;
            this.__domNode = RexToy.UI.Controls.DomNode.Create(new DomNodeCreateOptions()
            {
                tag = Tag.Div,
                className = "ui-radio",
                items = new DomNodeCreateOptions[]
                {
                    new DomNodeCreateOptions()
                    {
                        tag= Tag.Input,
                        type= InputType.Radio,
                        value= value
                    },
                    new DomNodeCreateOptions()
                    {
                        tag= Tag.Label,
                        hashName= "lbl",
                        className= "ui-btn ui-btn-icon-left ui-btn-up-c",
                        items= new DomNodeCreateOptions[] {
                            new DomNodeCreateOptions()
                            {
                                tag= Tag.Span,
                                hashName= "spn",
                                className= "ui-btn-inner",
                                items= new DomNodeCreateOptions[]{
                                    new DomNodeCreateOptions()
                                    {
                                        tag= Tag.Span,
                                        className= "ui-btn-text",
                                        innerHTML= text
                                    },
                                    new DomNodeCreateOptions()
                                    {
                                        tag= Tag.Span,
                                        hashName= "check",
                                        className= "ui-icon ui-icon-radio-off"
                                    }
                                }
                            }
                        }
                    }
                }
            });
            this.__domNode.AttachEvent(DomEvent.OnClick, new Action(this.OnClick).Bind(this));
            this.__domNode.AttachEvent(DomEvent.OnDoubleClick, new Action(this.OnClick).Bind(this));
            this.__domNode.AttachEvent(DomEvent.OnMouseOver, new Action(this.OnMouseOver).Bind(this));
            this.__domNode.AttachEvent(DomEvent.OnMouseOut, new Action(this.OnMouseOut).Bind(this));
        }

        private void OnMouseOver()
        {
            if (this.__enable)
            {
                var l = this.__domNode.SearchByName("lbl");
                l.ReplaceClass("ui-btn-up-c", "ui-btn-hover-c");
            }
        }

        private void OnMouseOut()
        {
            if (this.__enable)
            {
                var l = this.__domNode.SearchByName("lbl");
                l.ReplaceClass("ui-btn-hover-c", "ui-btn-up-c");
            }
        }

        private void OnClick()
        {
            if (this.__enable)
            {
                this.Checked = true;
                this.__FireEvent("onchange");
            }
        }

        public override void SetFirst(bool first)
        {
            var spn = this.__domNode.SearchByName("spn");
            var lbl = this.__domNode.SearchByName("lbl");
            if (first)
            {
                spn.AppendClass("ui-corner-top");
                lbl.AppendClass("ui-corner-top");
            }
            else
            {
                spn.RemoveClass("ui-corner-top");
                lbl.RemoveClass("ui-corner-top");
            }
        }

        public override void SetLast(bool last)
        {
            var spn = this.__domNode.SearchByName("spn");
            var lbl = this.__domNode.SearchByName("lbl");
            if (last)
            {
                spn.AppendClass("ui-corner-bottom");
                lbl.AppendClass("ui-corner-bottom");
            }
            else
            {
                spn.RemoveClass("ui-corner-bottom");
                lbl.RemoveClass("ui-corner-bottom");
            }
        }
    }
}
